<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<title>Equality constrained norm minimization.</title>
<link rel="canonical" href="http://cvxr.com/cvx/examples/html/equality_constr_norm_min.html">
<link rel="stylesheet" href="../examples.css" type="text/css">
</head>
<body>
<div id="header">
<h1>Equality constrained norm minimization.</h1>
Jump to:&nbsp;&nbsp;&nbsp;&nbsp;
<a href="#source">Source code</a>&nbsp;&nbsp;&nbsp;&nbsp;
<a href="#output">Text output</a>
&nbsp;&nbsp;&nbsp;&nbsp;
Plots
&nbsp;&nbsp;&nbsp;&nbsp;<a href="../index.html">Library index</a>
</div>
<div id="content">
<a id="source"></a>
<pre class="codeinput">
<span class="comment">% This script constructs a random equality-constrained norm minimization</span>
<span class="comment">% problem and solves it using CVX. You can also change p to +2 or +Inf</span>
<span class="comment">% to produce different results. Alternatively, you an replace</span>
<span class="comment">%     norm( A * x - b, p )</span>
<span class="comment">% with</span>
<span class="comment">%     norm_largest( A * x - b, 'largest', p )</span>
<span class="comment">% for 1 &lt;= p &lt;= 2 * n.</span>

<span class="comment">% Generate data</span>
p = 1;
n = 10; m = 2*n; q=0.5*n;
A = randn(m,n);
b = randn(m,1);
C = randn(q,n);
d = randn(q,1);

<span class="comment">% Create and solve problem</span>
cvx_begin
   variable <span class="string">x(n)</span>
   dual <span class="string">variable</span> <span class="string">y</span>
   minimize( norm( A * x - b, p ) )
   subject <span class="string">to</span>
        y : C * x == d;
cvx_end

<span class="comment">% Display results</span>
disp( sprintf( <span class="string">'norm(A*x-b,%g):'</span>, p ) );
disp( [ <span class="string">'   ans   =   '</span>, sprintf( <span class="string">'%7.4f'</span>, norm(A*x-b,p) ) ] );
disp( <span class="string">'Optimal vector:'</span> );
disp( [ <span class="string">'   x     = [ '</span>, sprintf( <span class="string">'%7.4f '</span>, x ), <span class="string">']'</span> ] );
disp( <span class="string">'Residual vector:'</span> );
disp( [ <span class="string">'   A*x-b = [ '</span>, sprintf( <span class="string">'%7.4f '</span>, A*x-b ), <span class="string">']'</span> ] );
disp( <span class="string">'Equality constraints:'</span> );
disp( [ <span class="string">'   C*x   = [ '</span>, sprintf( <span class="string">'%7.4f '</span>, C*x ), <span class="string">']'</span> ] );
disp( [ <span class="string">'   d     = [ '</span>, sprintf( <span class="string">'%7.4f '</span>, d   ), <span class="string">']'</span> ] );
disp( <span class="string">'Lagrange multiplier for C*x==d:'</span> );
disp( [ <span class="string">'   y     = [ '</span>, sprintf( <span class="string">'%7.4f '</span>, y ), <span class="string">']'</span> ] );
</pre>
<a id="output"></a>
<pre class="codeoutput">
 
Calling SDPT3: 50 variables, 25 equality constraints
------------------------------------------------------------

 num. of constraints = 25
 dim. of socp   var  = 40,   num. of socp blk  = 20
 dim. of free   var  = 10 *** convert ublk to lblk
*******************************************************************
   SDPT3: Infeasible path-following algorithms
*******************************************************************
 version  predcorr  gam  expon  scale_data
    NT      1      0.000   1        0    
it pstep dstep pinfeas dinfeas  gap      prim-obj      dual-obj    cputime
-------------------------------------------------------------------
 0|0.000|0.000|8.3e-01|2.9e+01|1.5e+04| 8.471540e+01  0.000000e+00| 0:0:00| chol  1  1 
 1|1.000|0.964|3.1e-06|1.1e+00|2.8e+02| 1.464735e+02  1.231256e+01| 0:0:00| chol  1  1 
 2|0.961|0.990|6.9e-07|2.0e-02|1.0e+01| 2.225613e+01  1.265515e+01| 0:0:00| chol  1  1 
 3|0.935|0.790|2.0e-05|5.0e-03|1.8e+00| 1.789246e+01  1.620084e+01| 0:0:00| chol  1  1 
 4|0.862|0.455|2.3e-05|2.8e-03|7.7e-01| 1.736289e+01  1.665120e+01| 0:0:00| chol  1  1 
 5|1.000|0.442|1.3e-07|1.6e-03|3.8e-01| 1.725395e+01  1.690178e+01| 0:0:00| chol  1  1 
 6|1.000|0.824|3.0e-08|2.7e-04|6.4e-02| 1.722824e+01  1.716909e+01| 0:0:00| chol  1  1 
 7|0.988|0.777|5.9e-09|6.1e-05|1.4e-02| 1.722561e+01  1.721288e+01| 0:0:00| chol  1  1 
 8|1.000|0.283|1.3e-09|4.4e-05|9.4e-03| 1.722491e+01  1.721622e+01| 0:0:00| chol  1  1 
 9|1.000|0.280|4.7e-10|3.1e-05|6.7e-03| 1.722479e+01  1.721858e+01| 0:0:00| chol  1  1 
10|1.000|0.688|1.7e-10|9.8e-06|2.1e-03| 1.722470e+01  1.722275e+01| 0:0:00| chol  1  1 
11|0.993|0.866|4.6e-11|1.4e-05|3.7e-04| 1.722465e+01  1.722439e+01| 0:0:00| chol  1  1 
12|0.988|0.988|1.9e-12|2.5e-06|2.0e-05| 1.722465e+01  1.722465e+01| 0:0:00| chol  1  1 
13|1.000|0.944|1.5e-13|1.4e-07|1.2e-06| 1.722465e+01  1.722465e+01| 0:0:00| chol  1  1 
14|0.575|0.944|6.6e-14|8.1e-09|9.7e-08| 1.722465e+01  1.722465e+01| 0:0:00|
  stop: max(relative gap, infeasibilities) &lt; 1.49e-08
-------------------------------------------------------------------
 number of iterations   = 14
 primal objective value =  1.72246500e+01
 dual   objective value =  1.72246500e+01
 gap := trace(XZ)       = 9.66e-08
 relative gap           = 2.72e-09
 actual relative gap    = 1.18e-09
 rel. primal infeas     = 6.57e-14
 rel. dual   infeas     = 8.07e-09
 norm(X), norm(y), norm(Z) = 7.5e+00, 6.8e+00, 6.2e+00
 norm(A), norm(b), norm(C) = 2.4e+01, 5.8e+00, 5.5e+00
 Total CPU time (secs)  = 0.17  
 CPU time per iteration = 0.01  
 termination code       =  0
 DIMACS: 1.3e-13  0.0e+00  2.2e-08  0.0e+00  1.2e-09  2.7e-09
-------------------------------------------------------------------
------------------------------------------------------------
Status: Solved
Optimal value (cvx_optval): +17.2247
 
norm(A*x-b,1):
   ans   =   17.2246
Optimal vector:
   x     = [ -0.5253 -0.6745  0.3881 -0.3432  0.6278 -0.7627  0.0400  0.6648  1.0041 -0.4625 ]
Residual vector:
   A*x-b = [  0.0000 -0.0000 -0.8708 -1.0281 -0.0000  1.5043  0.0000  1.3248  0.2570 -2.1089  2.3600  0.7021 -1.2957  0.9965  0.1152  0.8386 -2.2360  0.0000 -1.1761 -0.4105 ]
Equality constraints:
   C*x   = [ -0.0798  0.7377  1.5352  1.2677  1.9951 ]
   d     = [ -0.0798  0.7377  1.5352  1.2677  1.9951 ]
Lagrange multiplier for C*x==d:
   y     = [  2.2526  0.2934  1.6808 -0.2579  4.4457 ]
</pre>
</div>
</body>
</html>